<template>
	<view>
		<view>
		    <uni-calendar 
		    :insert="true"
		    :lunar="true" 
			@change="change($event)"
			:selected="allDataEnd"
		     />
		</view>
		<TodoItemContainer title="DDL事件" v-show="displayDDL">
			<MyRectangle v-for="item in DDLData" :data="item" @click="goToDetail(item.id)"/>
		</TodoItemContainer>
		<TodoItemContainer title="正在进行事件" v-show="displayDoing">
			<MyRectangle v-for="item in doingData" :data="item" @click="goToDetail(item.id)"/>
		</TodoItemContainer>
	</view>
</template>

<script setup>
import {onShow} from "@dcloudio/uni-app";
import db from '../../util/db';
import { ref } from "vue";
import mydate from '../../util/mydate';
// 在打开时初始化数据内容
const allData = ref();
const allDataEnd = ref();
function saveEnd()
{
	allDataEnd.value = [];
	for(let i in allData.value)
	{
		allDataEnd.value.push({date:allData.value[i].end,info:"DDL"});
	}
	console.log(allDataEnd.value);
}
// 生命周期钩子触发
onShow(async()=>{
	await db.openDatabase();
	allData.value = await db.selectEventAndDate();
	console.log(allData.value);
	await db.closeDatabase();
	saveEnd();
	getDDLAndDoing(mydate.todayString());
})
// 定义当天的DDL数据内容
// 定义正在进行的内容
const doingData = ref();
const displayDoing = ref(true);
const DDLData = ref();
const displayDDL = ref(true);
function getDDLAndDoing(date){
	DDLData.value = [];
	doingData.value = [];
	for(let item in allData.value)
	{
		console.log(date," ",allData.value[item].end)
		if(allData.value[item].end == date)
		{
			console.log("可行");
			DDLData.value.push(allData.value[item]);
		}
		let days = mydate.dayComputed(allData.value[item].begin,date);
		if(days > 0 && 
		days < mydate.dayComputed(allData.value[item].begin,allData.value[item].end))
		{
			doingData.value.push(allData.value[item]);
		}
	}
	console.log(doingData.value);
	displayDDL.value = true;
	if(DDLData.value.length == 0)
	{
		displayDDL.value = false;
	}
	displayDoing.value = true;
	if(doingData.value.length == 0)
	{
		displayDoing.value = false;
	}
}
// 在日历表改变时触发
// 定义记忆和计数用于判断是否需要跳转
const memery = ref("");
const count = ref(0);
// calender事件触发
function change(e)
{
	if(memery.value!=e.fulldate)
	{
		memery.value = e.fulldate;
		count.value = 0;
	}
	else{
		count.value++;
		if(count.value>=1)
		{
			console.log("跳转");
			uni.navigateTo({
				url:`/pages/addevent/addevent?date=${memery.value}`,
			})
		}
	}
	console.log(e);
	getDDLAndDoing(e.fulldate);
}
// 在单元项点击时进入
function goToDetail(id)
{
	const queryString = `id=${id}`;
	uni.navigateTo({
		url:"/pages/eventDetail/eventDetail?"+queryString,
	});
}
</script>

<style>

</style>